#pragma once
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// 防止在绑核+RT条件下出现死循环，其他线程得不到调度, sleep 1 ms

// struct pollfd
// {
// int fd; /* File descriptor to poll. */
// short int events; /* Types of events poller cares about. */
// short int revents; /* Types of events that actually occurred. */
// };
// 常和 pipe2() 搭配
// timer_fd_ = timerfd_create(kClockId, TFD_NONBLOCK | TFD_CLOEXEC);  // PRQA S
// 3710,3720

int testPoll() {
  struct pollfd fds[1];
  char buffer[1024];
  ssize_t bytesRead;

  // 监视标准输入（stdin）是否可读
  fds[0].fd = STDIN_FILENO;
  fds[0].events = POLLIN;

  while (1) {
    // 等待输入或超时（1000毫秒）
    int ready = poll(fds, 1, 1000);

    if (ready == -1) {
      perror("poll");
      exit(EXIT_FAILURE);
    }

    if (ready > 0 && (fds[0].revents & POLLIN)) {
      // 读取输入
      bytesRead = read(STDIN_FILENO, buffer, sizeof(buffer) - 1);

      if (bytesRead > 0) {
        buffer[bytesRead] = '\0'; // 添加字符串终止符
        printf("You entered: %s", buffer);
      }
    }
  }

  return 0;
}